package com.xuecheng.content.domain.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.domain.mapper.CourseCategoryMapper;
import com.xuecheng.content.domain.service.CourseCategoryService;
import com.xuecheng.content.domain.model.entity.CourseCategory;
import com.xuecheng.content.domain.model.dto.CourseCategoryTreeDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {

    @Autowired
    private CourseCategoryMapper courseCategoryMapper;

    @Override
    public List<CourseCategoryTreeDto> queryTreeNodes(String id) {
        List<CourseCategoryTreeDto> treeNodes = courseCategoryMapper.selectTreeNodes(id);
        Map<String, List<CourseCategoryTreeDto>> groupMap = treeNodes.stream()
                .collect(Collectors.groupingBy(CourseCategoryTreeDto::getParentid));

        return treeNodes.stream()
                .filter(item -> item.getParentid().equals(id))
                .map(item -> {
                    item.setChildrenTreeNodes(groupMap.get(item.getId()));
                    return item;
                }).collect(Collectors.toList());
    }
}
